//
//  ViewController.swift
//  picPost
//
//  Created by dream on 2024/1/5.
//

import UIKit
import SnapKit
import TYAlertController

class ViewController: UIViewController {

    @IBOutlet weak var lineView: UIView!
    
    @IBOutlet weak var agreeButton: UIButton!
    
    var agreeLayer : CAGradientLayer?
    
    var agree = false {
        didSet {
//            guard let agreeView = self.view.viewWithTag(20) else { return }
//            let anim = CABasicAnimation()
//            anim.keyPath = "opacity"
//            anim.fromValue = agree ? 0 : 1
//            anim.toValue = agree ? 1 : 0
//            anim.duration = 0.5
//            anim.isRemovedOnCompletion = false
//            anim.fillMode = .forwards
//            agreeView.layer.sublayers?.first?.add(anim, forKey: "opacity")
            
            agreeLayer?.opacity = agree ? 1 : 0
            agreeButton.isEnabled = agree
        }
    }
    
    var index = 0
    
    @IBAction func termAgree(_ sender: UIButton) {
        agree = !agree
        
        index += 1
        if index > 1 {
            let view = Bundle.main.loadNibNamed("TermsView", owner: nil)?.first as? TermsView
            guard let alertVc = TYAlertController(alert: view, preferredStyle: .actionSheet) else {return}
            present(alertVc, animated: true)
        }
    }
    
    @IBAction func agree(_ sender: UIButton) {
        let view = Bundle.main.loadNibNamed("InputInfoView", owner: nil)?.first as? InputInfoView
        guard let alertVc = TYAlertController(alert: view, preferredStyle: .actionSheet) else {return}
        present(alertVc, animated: true)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
//        UIFont.familyNames.forEach { name in
//            let names = UIFont.fontNames(forFamilyName: name)
//            print("\(name)---\(names)")
//        }
        
        
        self.view.layoutIfNeeded()
        
        guard let agreeView = self.view.viewWithTag(20) else { return }
        
        UIView.animate(withDuration: 2) {
            self.lineView.snp.updateConstraints { make in
                make.width.equalToSuperview()
            }
            self.view.layoutIfNeeded()
        } completion: { _ in
            UIView.animate(withDuration: 0.5) {
                agreeView.alpha = 1
                self.view.layoutIfNeeded()
            }
        }
        
        guard let layer = agreeView.layer.sublayers?.first as? CAGradientLayer else { return }        
        agreeLayer = layer
        agreeLayer?.opacity = 0
    }


}

